<?php
namespace app\mall\logic;

use think\facade\Db;

class ShoppingCartLogic{

    /**
     * 加入购物车
     * @param int $uid 用户id
     * @param int $id 抽奖信息id one_goods_lucky_draw表
     * @param int $purchase_quantity 购买数量
     * @return array
     */
    public function add_shopping_cart(int $uid ,int $id ,int $purchase_quantity):array{
        $goods_lucky_draw = Db::table('one_goods_lucky_draw')
            ->where('id','=',$id)
            ->find();
        if (empty($goods_lucky_draw)) return array('code'=>500,'msg'=>'商品不存在');
        if (!empty($goods_lucky_draw['is_draw_a_lottery'])) return array('code'=>500,'msg'=>'该商品已开奖');
        if ($goods_lucky_draw['people_number'] <= $goods_lucky_draw['participants_number']) return array('code'=>500,'msg'=>'库存不足');
        $arr = array(
            'u_id' => $uid,
            'goods_lucky_draw_id' => $id
        );
        $is = Db::table('one_shopping_cart')
            ->where($arr)
            ->find();
        //查看购物车是否有该商品，有则更新数量，否则新增
        if ($is){
            $res = Db::table('one_shopping_cart')
                ->where($arr)
                ->inc('purchase_quantity',$purchase_quantity)
                ->update();
        }else{
            $arr['purchase_quantity'] = $purchase_quantity;
            $res = Db::table('one_shopping_cart')->insert($arr);
        }
        if ($res) return array('code'=>200,'msg'=>'加入购物车成功');
        return array('code'=>500,'msg'=>'加入购物车失败');
    }

    /**
     * 购物车列表
     * @param int $uid
     */
    public function shopping_cart_list(int $uid){
        return Db::view('one_shopping_cart','goods_lucky_draw_id ,purchase_quantity')
            ->where('u_id','=',$uid)
            ->view('one_goods_lucky_draw','id ,unit_price ,people_number ,participants_number ','one_shopping_cart.goods_lucky_draw_id = one_goods_lucky_draw.id')
            ->view('one_goods','goods_name ,cover','one_goods_lucky_draw.goods_id = one_goods.id')
            ->order('one_shopping_cart.create_time','desc')
            ->select();
    }

    /**
     * 删除购物车
     * @param int $uid
     * @param int $id 抽奖信息id one_goods_lucky_draw表
     * @return bool
     */
    public static function del_shopping_cart(int $uid ,int $id):bool{
        $res = Db::table('one_shopping_cart')
            ->where(array(
                'u_id' => $uid,
                'goods_lucky_draw_id' => $id
            ))->delete();
        if ($res) return true;
        return false;
    }

    /**
     * 购物车商品 增和减
     * @param int $uid 用户id
     * @param int $id 抽奖信息id one_goods_lucky_draw表
     * @param int $operate 操作类型 1增 2减
     * @param int $purchase_quantity 数量
     * @return array
     */
    public function shopping_cart_inc_dec(int $uid ,int $id ,int $operate ,int $purchase_quantity):array{
        $where = array(
            'u_id' => $uid,
            'goods_lucky_draw_id' => $id
        );
        switch (true){
            case $operate == 1:
                $res = Db::table('one_shopping_cart')
                    ->where($where)
                    ->inc('purchase_quantity',$purchase_quantity)
                    ->update();
                break;
            case $operate == 2:
                $surplus_purchase_quantity = Db::table('one_shopping_cart')
                    ->where($where)
                    ->value('purchase_quantity');
                if (empty($surplus_purchase_quantity) || $surplus_purchase_quantity <= $purchase_quantity) return array('code'=>500,'msg'=>'数量不可低于1');
                $res = Db::table('one_shopping_cart')
                    ->where($where)
                    ->dec('purchase_quantity',$purchase_quantity)
                    ->update();
                break;
            default :
                return array('code'=>500,'msg'=>'操作类型错误');
        }
        if ($res) return array('code'=>200,'msg'=>'操作成功');
        return array('code'=>500,'msg'=>'操作失败');
    }

}